﻿using Brilliant.Data.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace LogViewer
{
    public partial class FrmMain : Form
    {
        private List<LogInfo> _logList;
        private LogInfo _selectLog;
        private StringBuilder sbParam = new StringBuilder();

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
        }

        private void itemOpenLog_Click(object sender, EventArgs e)
        {
            if (dialogOpenFile.ShowDialog() == DialogResult.OK)
            {
                string log = String.Format("[{0}]", File.ReadAllText(dialogOpenFile.FileName));
                _logList = JsonSerializer.JSDeSerialize<List<LogInfo>>(log);
                BindData();
            }
        }

        private void BindData()
        {
            lvLogs.SelectedItems.Clear();
            lvLogs.Items.Clear();
            int i = 0;
            foreach (LogInfo log in _logList)
            {
                ListViewItem item = new ListViewItem(new string[] { log.Date, log.LogType, log.LogLevel, log.Message });
                item.ToolTipText = log.Message;
                lvLogs.Items.Add(item);
                if (i == 0)
                {
                    item.Selected = true;
                }
                i++;
            }
        }

        private void lvLogs_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            this._selectLog = _logList[e.ItemIndex];
            this.txtSQL.Text = _selectLog.CmdText;
            if (_selectLog.Parameters != null)
            {
                sbParam.Clear();
                foreach (var item in _selectLog.Parameters)
                {
                    sbParam.AppendFormat("参数：{0}\r\n", item.ParameterName);
                    sbParam.AppendFormat("    类型：{0}\r\n", item.DbType);
                    sbParam.AppendFormat("    长度：{0}\r\n", item.Size);
                    sbParam.AppendFormat("    值：{0}\r\n", item.Value);
                }
                this.txtParams.Text = sbParam.ToString();
            }
        }
    }
}
